package web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import modelo.mMensajes;
import modelo.mSabio;
import negocio.nSabio;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SabioController {
	
	@RequestMapping(value = "/sabios")
	protected ModelAndView terminar(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String pagina = "";
		String accion = request.getParameter("ind");
		System.out.println("ind = " + accion);
		ModelAndView rpta = null;
		try{
			if(accion.equals("1")){
				// listado de Sabios
				pagina = "proyLstSabio";
				nSabio nSa = new nSabio();
				rpta = new ModelAndView(pagina,"LISTA",nSa.listaSabio());
			}else if(accion.equals("M")){
				// ir a menu de administrador
				pagina = "proyMenu";
				rpta = new ModelAndView(pagina);
			}else if(accion.equals("R")){
				// registro de Sabio
				pagina = "proyRegSabio";
				nSabio nSa = new nSabio();
				rpta = new ModelAndView(pagina,"BANCO",nSa.listaBanco());
			}
		}catch(Exception e){
			pagina = "proyMenu";
			rpta = new ModelAndView(pagina);
		}
		return rpta;
	}
	
	@RequestMapping(value = "/regSabio")
	protected ModelAndView grabarSabio(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String pagina = "proyMensajes";
		
		mSabio mSa = new mSabio();
		mSa.setNombre(request.getParameter("txNombre").trim());
		mSa.setApPaterno(request.getParameter("txPaterno").trim());
		mSa.setApMaterno(request.getParameter("txMaterno").trim());
		mSa.setSexo(request.getParameter("rdSexo").trim());
		mSa.setTipDoc(request.getParameter("slDocI").trim());
		mSa.setNumDoc(request.getParameter("txDNI").trim());
		mSa.setEmail(request.getParameter("txEmail").trim());
		mSa.setCelular(request.getParameter("txCelu").trim());
		mSa.setDireccion(request.getParameter("txDirec").trim());
		mSa.setFecNac(request.getParameter("txFNac"));
		mSa.setPwd(request.getParameter("txPwd"));
		
		mSa.setApelativo(request.getParameter("txApelativo"));
		mSa.setResumen(request.getParameter("txResumen"));
		mSa.setSitioWeb(request.getParameter("txSitio"));
		mSa.setIdBanco(Integer.parseInt(request.getParameter("txBanco").trim()));
		mSa.setCtacte(request.getParameter("txCtaCte"));
		
		nSabio nSa = new nSabio();
		String rpta = nSa.insertar(mSa);
		mMensajes objM = new mMensajes();
		objM.setTexto(rpta);
		if(rpta.equals("GRABADO CORRECTAMENTE")){
			objM.setPagina("1");
			objM.setError(0);
		}else{
			objM.setPagina("R");
			objM.setError(1);
		}
		System.out.println(objM.getPagina());
		return new ModelAndView(pagina,"MENSAJES",objM);
	}
}
